/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtGui>
#include <QMessageBox>

//#include "renderthread.h"
#include "window.h"




//! [0]
const int IdRole = Qt::UserRole;
//! [0]

//! [1]
Window::Window()
{

    if (this->objectName().isEmpty())
            this->setObjectName(QString::fromUtf8("this"));
        this->resize(661, 699);

        centralWidget = new QWidget(this);
        QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        sizePolicy.setHorizontalStretch(0);
        sizePolicy.setVerticalStretch(2);
        sizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
        this->setSizePolicy(sizePolicy);
        gridLayout = new QGridLayout(centralWidget);
        gridLayout->setSpacing(6);
        gridLayout->setContentsMargins(11, 11, 11, 11);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));

        verticalLayout_3 = new QVBoxLayout();
        verticalLayout_3->setSpacing(6);
        verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));

        horizontalLayout_3 = new QHBoxLayout();

        methodComboBox = new QComboBox;

        methodComboBox->addItem(tr("Arc-Length"), ArcLen);
        methodComboBox->addItem(tr("Arc-Length_sqrt"), ArcLenSqrt);
        methodComboBox->addItem(tr("Arc-Length_4th"), ArcLenFour);
        methodComboBox->addItem(tr("Local Triangle Areas"), Area);
        methodComboBox->addItem(tr("Local Triangle Areas_Abs"), AreaAbs);
        methodComboBox->addItem(tr("Local Triangle Areas_Square"), AreaSquare);
        methodComboBox->addItem(tr("Multiple Local Area"), AreaMulti);
        methodComboBox->addItem(tr("Bilateral Normals"), BilateralNormal);
        methodComboBox->addItem(tr("COS_PI"), COS_PI);
        methodComboBox->addItem(tr("Total Area"), TotalArea);
        methodComboBox->addItem(tr("Turning Angle"), TurningAngle);
        methodComboBox->addItem(tr("Discrete Curvature Normal"), DiscreteCurvatureNormal);
        QSizePolicy sizePolicy6(QSizePolicy::Expanding, QSizePolicy::Minimum);
        sizePolicy6.setHorizontalStretch(1);
        sizePolicy6.setVerticalStretch(0);
        sizePolicy6.setHeightForWidth(methodComboBox->sizePolicy().hasHeightForWidth());
        methodComboBox->setSizePolicy(sizePolicy6);

        methodLabel = new QLabel(tr("&Method:"));
        methodLabel->setBuddy(methodComboBox);

        horizontalLayout_3->addWidget(methodLabel);
        horizontalLayout_3->addWidget(methodComboBox);
        verticalLayout_3->addLayout(horizontalLayout_3);

        horizontalLayout = new QHBoxLayout();
        horizontalLayout->setSpacing(6);
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        verticalLayout_2 = new QVBoxLayout();
        verticalLayout_2->setSpacing(6);
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
        label = new QLabel(centralWidget);
        label->setObjectName(QString::fromUtf8("label"));

        verticalLayout_2->addWidget(label);

        label_2 = new QLabel(centralWidget);
        label_2->setObjectName(QString::fromUtf8("label_2"));

        verticalLayout_2->addWidget(label_2);

        label_3 = new QLabel(centralWidget);
        label_3->setObjectName(QString::fromUtf8("label_3"));

        verticalLayout_2->addWidget(label_3);

        label_4 = new QLabel(centralWidget);
        label_4->setObjectName(QString::fromUtf8("label_4"));
        verticalLayout_2->addWidget(label_4);

        label_VarVertex = new QLabel(centralWidget);
        label_VarVertex->setObjectName(QString::fromUtf8("label_VarVertex"));
        verticalLayout_2->addWidget(label_VarVertex);

        label_VarNormal = new QLabel(centralWidget);
        label_VarNormal->setObjectName(QString::fromUtf8("label_VarNormal"));
        verticalLayout_2->addWidget(label_VarNormal);

        horizontalLayout->addLayout(verticalLayout_2);

        verticalLayout = new QVBoxLayout();
        verticalLayout->setSpacing(6);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        Similarity_lineEdit = new QLineEdit(centralWidget);
        Similarity_lineEdit->setObjectName(QString::fromUtf8("Similarity_lineEdit"));
        QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Expanding);
        sizePolicy1.setHorizontalStretch(0);
        sizePolicy1.setVerticalStretch(0);
        sizePolicy1.setHeightForWidth(Similarity_lineEdit->sizePolicy().hasHeightForWidth());
        Similarity_lineEdit->setSizePolicy(sizePolicy1);

        verticalLayout->addWidget(Similarity_lineEdit);

        Time_lineEdit = new QLineEdit(centralWidget);
        Time_lineEdit->setObjectName(QString::fromUtf8("Time_lineEdit"));
        sizePolicy1.setHeightForWidth(Time_lineEdit->sizePolicy().hasHeightForWidth());
        Time_lineEdit->setSizePolicy(sizePolicy1);

        verticalLayout->addWidget(Time_lineEdit);

        Iteration_lineEdit = new QLineEdit(centralWidget);
        Iteration_lineEdit->setObjectName(QString::fromUtf8("Iteration_lineEdit"));
        sizePolicy1.setHeightForWidth(Iteration_lineEdit->sizePolicy().hasHeightForWidth());
        Iteration_lineEdit->setSizePolicy(sizePolicy1);

        verticalLayout->addWidget(Iteration_lineEdit);

        Triangle_spinBox = new QSpinBox(centralWidget);
        Triangle_spinBox->setObjectName(QString::fromUtf8("spinBox"));
        sizePolicy1.setHeightForWidth(Triangle_spinBox->sizePolicy().hasHeightForWidth());
        Triangle_spinBox->setSizePolicy(sizePolicy1);

        verticalLayout->addWidget(Triangle_spinBox);

        VertexVariance_lineEdit = new QLineEdit(centralWidget);
        VertexVariance_lineEdit->setObjectName(QString::fromUtf8("VertexVariance_lineEdit"));
        sizePolicy1.setHeightForWidth(VertexVariance_lineEdit->sizePolicy().hasHeightForWidth());
        VertexVariance_lineEdit->setSizePolicy(sizePolicy1);
        verticalLayout->addWidget(VertexVariance_lineEdit);


        NormalVariance_lineEdit = new QLineEdit(centralWidget);
        NormalVariance_lineEdit->setObjectName(QString::fromUtf8("VertexVariance_lineEdit"));
        sizePolicy1.setHeightForWidth(NormalVariance_lineEdit->sizePolicy().hasHeightForWidth());
        NormalVariance_lineEdit->setSizePolicy(sizePolicy1);
        verticalLayout->addWidget(NormalVariance_lineEdit);

        horizontalLayout->addLayout(verticalLayout);


        verticalLayout_3->addLayout(horizontalLayout);



        verticalSpacer_2 = new QSpacerItem(20, 50, QSizePolicy::Minimum, QSizePolicy::Fixed);

        verticalLayout_3->addItem(verticalSpacer_2);






        horizontalLayout_2 = new QHBoxLayout();
        horizontalLayout_2->setSpacing(0);
        horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
        horizontalLayout_2->setContentsMargins(8, 0, -1, -1);
        Explicit_radioButton = new QRadioButton(centralWidget);
        Explicit_radioButton->setObjectName(QString::fromUtf8("Explicit_radioButton"));
        QSizePolicy sizePolicy2(QSizePolicy::Minimum, QSizePolicy::Preferred);
        sizePolicy2.setHorizontalStretch(0);
        sizePolicy2.setVerticalStretch(0);
        sizePolicy2.setHeightForWidth(Explicit_radioButton->sizePolicy().hasHeightForWidth());
        Explicit_radioButton->setSizePolicy(sizePolicy2);

        horizontalLayout_2->addWidget(Explicit_radioButton);

        Implicit_radioButton = new QRadioButton(centralWidget);
        Implicit_radioButton->setObjectName(QString::fromUtf8("Implicit_radioButton"));
        sizePolicy2.setHeightForWidth(Implicit_radioButton->sizePolicy().hasHeightForWidth());
        Implicit_radioButton->setSizePolicy(sizePolicy2);

        horizontalLayout_2->addWidget(Implicit_radioButton);




        verticalLayout_3->addLayout(horizontalLayout_2);

        verticalSpacer = new QSpacerItem(20, 50, QSizePolicy::Minimum, QSizePolicy::Fixed);

        verticalLayout_3->addItem(verticalSpacer);



        saveButton = new QPushButton(centralWidget);
        saveButton->setObjectName(QString::fromUtf8("saveButton"));
        QSizePolicy sizePolicy3(QSizePolicy::Minimum, QSizePolicy::Preferred);
        sizePolicy3.setHorizontalStretch(0);
        sizePolicy3.setVerticalStretch(0);
        sizePolicy3.setHeightForWidth(saveButton->sizePolicy().hasHeightForWidth());
        saveButton->setSizePolicy(sizePolicy3);

        verticalLayout_3->addWidget(saveButton);

        runButton = new QPushButton(centralWidget);
        runButton->setObjectName(QString::fromUtf8("runButton"));
        QSizePolicy sizePolicy4(QSizePolicy::Minimum, QSizePolicy::Expanding);
        sizePolicy4.setHorizontalStretch(0);
        sizePolicy4.setVerticalStretch(0);
        sizePolicy4.setHeightForWidth(runButton->sizePolicy().hasHeightForWidth());
        runButton->setSizePolicy(sizePolicy4);

        verticalLayout_3->addWidget(runButton);

        stopButton = new QPushButton(centralWidget);
        stopButton->setObjectName(QString::fromUtf8("stopButton"));
        sizePolicy4.setHeightForWidth(stopButton->sizePolicy().hasHeightForWidth());
        stopButton->setSizePolicy(sizePolicy4);

        verticalLayout_3->addWidget(stopButton);


        gridLayout->addLayout(verticalLayout_3, 0, 0, 1, 1);

        renderArea = new RenderArea(centralWidget);
        thread = new RenderThread;
        //widget->setObjectName(QString::fromUtf8("widget"));
        QSizePolicy sizePolicy5(QSizePolicy::Expanding, QSizePolicy::Expanding);
        sizePolicy5.setHorizontalStretch(17);
        sizePolicy5.setVerticalStretch(0);
        sizePolicy5.setHeightForWidth(renderArea->sizePolicy().hasHeightForWidth());
        renderArea->setSizePolicy(sizePolicy5);

        gridLayout->addWidget(renderArea, 0, 1, 1, 1);

        this->setCentralWidget(centralWidget);

        menus = new QMenuBar(this);
        menus->setGeometry(QRect(0, 0, 688, 21));
        this->createMenu(menus);
        this->setMenuBar(menus);


        statusBar = new QStatusBar(this);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        this->setStatusBar(statusBar);

        statusBar->showMessage(tr("Ready"), 2000);

        this->setWindowTitle(QApplication::translate("Polygon Evolution", "Polygon Evolution", 0, QApplication::UnicodeUTF8));
        label->setText(QApplication::translate("Polygon Evolution", "Similarity", 0, QApplication::UnicodeUTF8));
        label_2->setText(QApplication::translate("Polygon Evolution", "TimeStep", 0, QApplication::UnicodeUTF8));
        label_3->setText(QApplication::translate("Polygon Evolution", "Iterations", 0, QApplication::UnicodeUTF8));
        label_4->setText(QApplication::translate("Polygon Evolution", "Triangle No.", 0, QApplication::UnicodeUTF8));
        label_VarVertex->setText(QApplication::translate("Polygon Evolution", "Variance Vertex", 0, QApplication::UnicodeUTF8));
        label_VarNormal->setText(QApplication::translate("Polygon Evolution", "Variance Normal", 0, QApplication::UnicodeUTF8));
        Explicit_radioButton->setText(QApplication::translate("Polygon Evolution", "Explicit", 0, QApplication::UnicodeUTF8));
        Implicit_radioButton->setText(QApplication::translate("Polygon Evolution", "Implicit", 0, QApplication::UnicodeUTF8));
        saveButton->setText(QApplication::translate("Polygon Evolution", "Save", 0, QApplication::UnicodeUTF8));
        runButton->setText(QApplication::translate("Polygon Evolution", "Run", 0, QApplication::UnicodeUTF8));
        stopButton->setText(QApplication::translate("Polygon Evolution", "Stop", 0, QApplication::UnicodeUTF8));

        connect(runButton, SIGNAL(clicked()), this, SLOT(run()));
        connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));
        connect(saveButton, SIGNAL(clicked()), this, SLOT(saveParameters()));
        connect(this->methodComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(MethodChanged(int)));

        connect(this, SIGNAL(sendParameters(const Parameter *,MPolygon,std::vector<MPolygon> &,int)),
                thread, SLOT(getParameters(const Parameter *,MPolygon,std::vector<MPolygon> &,int)));

//        connect(thread, SIGNAL(evolvePolygon(std::vector<MPolygon> &)),
//                this, SLOT(updatePolygon(std::vector<MPolygon> &)));

        connect(thread, SIGNAL(evolvePolygon(MPolygon, int)),
                this, SLOT(updatePolygon(MPolygon, int)));

        connect(thread, SIGNAL(resetButton()), this, SLOT(Buttonreset()));
        connect(this, SIGNAL(resetThread()), thread, SLOT(threadReset()));
        connect(this, SIGNAL(toPlotPolygon()), renderArea, SLOT(forPlotPolygon()));

        isParamSet = false;
        Triangle_spinBox->setEnabled(false);
        VertexVariance_lineEdit->setEnabled(false);
        NormalVariance_lineEdit->setEnabled(false);
        this->initialButtons();


        this->setWindowState(Qt::WindowMaximized);
}

void Window::createMenu(QMenuBar *menus)
{
    loadAction = new QAction(tr("Load"), menus);
    loadAction->setStatusTip(tr("Load File "));
    closeAction = new QAction(tr("Close"), menus);
    saveAction = new QAction(tr("Save"), menus);
    FilledAction = new QAction(tr("Filled Polygon"), menus);
    WireAction = new QAction(tr("Wire Frame"), menus);
    ShowLabellingAction = new QAction(tr("Show Labelling"), menus);
    PointAction = new QAction(tr("Show Point"), menus);
    OriginalAction = new QAction(tr("Show Original Curve"), menus);
    outputAction = new QAction(tr("Output"), menus);
    drawAction = new QAction(tr("Draw"), menus);
    drawAction->setStatusTip(tr("Plot a new polygon"));
    reorderAction = new QAction(tr("Reorder"), menus);
    reorderAction->setStatusTip(tr("Reorder the polygon"));
    scaleAction = new QAction(tr("Scale"), menus);
    scaleAction->setStatusTip(tr("Rescale the domain"));
    refineAction = new QAction(tr("Subdivide"), menus);
    refineAction->setStatusTip(tr("Equally subdivide the polygon and Reset the initial polygon"));

    fileMenu = menus->addMenu(tr("File"));
    viewMenu = menus->addMenu(tr("View"));
    editMenu = menus->addMenu(tr("Edit"));
    fileMenu->addAction(drawAction);
    fileMenu->addAction(loadAction);
    fileMenu->addAction(saveAction);
    fileMenu->addAction(closeAction);


    viewMenu->addAction(WireAction);
    viewMenu->addAction(FilledAction);
    viewMenu->addAction(PointAction);
    viewMenu->addAction(ShowLabellingAction);
    viewMenu->addAction(OriginalAction);
    viewMenu->addSeparator();
    viewMenu->addAction(scaleAction);

    editMenu->addAction(refineAction);
    editMenu->addAction(reorderAction);
    editMenu->addAction(outputAction);



    FilledAction->setCheckable(true);
    WireAction->setCheckable(true);
    WireAction->setChecked(true);
    PointAction->setCheckable(true);
    ShowLabellingAction->setCheckable(true);
    OriginalAction->setCheckable(true);
    outputAction->setCheckable(true);
    connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
    connect(loadAction, SIGNAL(triggered()), this, SLOT(loadFile()));
    connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
    connect(drawAction, SIGNAL(triggered()), this, SLOT(plot()));

    connect(scaleAction, SIGNAL(triggered()), renderArea, SLOT(rescale()));

    connect(refineAction, SIGNAL(triggered()), this, SLOT(refine()));
    connect(reorderAction, SIGNAL(triggered()), this, SLOT(reorder()));

    connect(FilledAction, SIGNAL(triggered()), this, SLOT(setViewModel()));
    connect(WireAction, SIGNAL(triggered()), this, SLOT(setViewModel()));
    connect(ShowLabellingAction, SIGNAL(triggered()), this, SLOT(setViewModel()));
    connect(PointAction, SIGNAL(triggered()), this, SLOT(setViewModel()));
    connect(OriginalAction, SIGNAL(triggered()), this, SLOT(setViewModel()));
    connect(outputAction, SIGNAL(triggered()), this, SLOT(setOutput()));
}

void Window::initialButtons()
{

    this->runButton->setEnabled(isParamSet);
    this->stopButton->setEnabled(false);
    this->Implicit_radioButton->setChecked(isParamSet);
}

void Window::loadFile()
{
    QString fileName = QFileDialog::getOpenFileName(this);

    int no = 0;
    if(!fileName.isEmpty()){

        if(thread->hasData)
            thread->ClearData();

        if(renderArea->loadFile(fileName))
        {

            no = renderArea->getVerticesNumber();
            this->FileName = fileName;
        }
        else{
            this->statusBar->showMessage(tr("Something wrong!"));
        }

    }

    if(this->outputAction->isChecked())
        thread->setOutputFile(fileName);


    this->statusBar->showMessage(tr("%1 is loaded. The Polygon has %2").arg(fileName).arg(no));
    this->Triangle_spinBox->setMinimum(1);
    this->Triangle_spinBox->setMaximum( renderArea->getVerticesNumber()/2- 1);
    this->updateGeometry();
    renderArea->updateGL();
    renderArea->repaint();



}


void Window::setViewModel()
{
    if(renderArea->viewmod == RenderArea::Filled && WireAction->isChecked()){
        renderArea->viewmod = RenderArea::Wired;
        FilledAction->setChecked(false);
    }

    if(renderArea->viewmod == RenderArea::Wired && FilledAction->isChecked()){
        renderArea->viewmod = RenderArea::Filled;
        WireAction->setChecked(false);
    }


    renderArea->isShowPoint = this->PointAction->isChecked();

    renderArea->isShowOriginal = this->OriginalAction->isChecked();

    renderArea->isShowLabelling = this->ShowLabellingAction->isChecked();

    if(renderArea->viewmod == RenderArea::Filled)
        FilledAction->setChecked(true);

    if(renderArea->viewmod == RenderArea::Wired)
        WireAction->setChecked(true);

    renderArea->updateGL();

}

void Window::saveParameters()
{

    if(renderArea->hasData()==false){
        this->statusBar->showMessage(tr("No input polygon!"));
        return;
    }


    if(this->Similarity_lineEdit->isModified() &&
       this->Time_lineEdit->isModified()&&
       this->Iteration_lineEdit->isModified()){

        this->isParamSet = true;
        this->initialButtons();
        this->statusBar->showMessage(tr("Parameters setting is done!"));

        return;
    }

    this->statusBar->showMessage(tr("Some parameters are uninitialized"));

}


void Window::saveFile()   
{

    if(renderArea->hasData()==false){
        this->statusBar->showMessage(tr("No polygon!"));
        return;
    }

    QString fileName = QFileDialog::getSaveFileName(this);

    if(!fileName.isEmpty()){
        if(renderArea->saveFile(fileName))
        {

            statusBar->showMessage(tr("Save to %1 done!").arg(fileName));
        }
        else
             statusBar->showMessage(tr("Something Wrong"));
    }
}

void Window::setOutput(){
    if(!outputAction->isChecked())
        thread->OutputPerIter = false;
    else{
        thread->OutputPerIter = true;
        thread->setOutputFile(this->FileName);
    }
}

void Window::plot()
{
    statusBar->showMessage(tr("width %1 ; height %2").arg(renderArea->width()).arg(height()));
    thread->ClearData();
    renderArea->Segments.clear();
    emit this->toPlotPolygon();
}

void Window::stop()
{
    runButton->setEnabled(true);
    stopButton->setEnabled(false);


    thread->setStart(false);

    statusBar->showMessage(tr("Evolution is stopped. The new polygon has %1 points").arg(renderArea->getVerticesNumber()));

}
void Window::run()
{


    if(this->isParamSet == true){
        runButton->setEnabled(false);
        stopButton->setEnabled(true);
        thread->setStart(true);

        Parameter *para = new Parameter;
        para->MaxIte = this->Iteration_lineEdit->text().toInt();
        para->similarity = this->Similarity_lineEdit->text().toDouble();
        para->TimeStep = this->Time_lineEdit->text().toDouble();
        para->VarNormal = this->VertexVariance_lineEdit->text().toDouble();
        para->VarVertex = this->NormalVariance_lineEdit->text().toDouble();
        if(Triangle_spinBox->isEnabled() == true)
            para->Jump = this->Triangle_spinBox->text().toInt();
        para->isExplicit = this->Explicit_radioButton->isChecked();
        emit this->sendParameters(para,*(renderArea->iniPolygon),
                                  (renderArea->Polygons), methodComboBox->currentIndex());
    }

}

void Window::updatePolygon(MPolygon polygon, int k)
{

    if(k > renderArea->Polygons.size()-1)
    {
        for(int i = renderArea->Polygons.size(); i <= k; i++)
            renderArea->Polygons.push_back(polygon);
    }
    renderArea->Polygons[k].points.resize(polygon.points.size(), Point2d());
    for(int i = 0; i< polygon.PointNo(); i++)
    {
        renderArea->Polygons[k].points[i].x = polygon.points[i].x;
        renderArea->Polygons[k].points[i].y = polygon.points[i].y;

    }



    renderArea->updateGL();
    statusBar->showMessage(tr("Iteration : %1").arg(thread->Ite));

    renderArea->Segments.resize(thread->Joints.size(),Segment2d());
}

void Window::Buttonreset()
{
    runButton->setEnabled(true);
    stopButton->setEnabled(false);

    renderArea->Segments.resize(thread->Joints.size(),Segment2d());
    for(int i = 0; i< thread->Joints.size(); i++)
    {
        renderArea->Segments[i].A = thread->Polygons[thread->Joints[i][0]].points[thread->Joints[i][1]];
        renderArea->Segments[i].B = thread->Polygons[thread->Joints[i][2]].points[thread->Joints[i][3]];

    }
    renderArea->updateGL();
    emit resetThread();
}

void Window::refine()
{
    if(thread->Ite == 0)
        renderArea->refine();
}

void Window::reorder()
{
    std::vector<int> act;
    for(int k=0; k < renderArea->getPolygonNumber();k++)
        renderArea->Polygons[k].reorder(1e-6, act);
    renderArea->updateGL();
}

void Window::MethodChanged(int Index)
{
    switch(Index){
        case ArcLen:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        case ArcLenSqrt:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        case ArcLenFour:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        case Area:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        case AreaAbs:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        case AreaSquare:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        case AreaMulti:
            Triangle_spinBox->setEnabled(true);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        case BilateralNormal:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(true);
            NormalVariance_lineEdit->setEnabled(true);
            break;
        case COS_PI:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        case TotalArea:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
        case TurningAngle:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        case DiscreteCurvatureNormal:
            Triangle_spinBox->setEnabled(false);
            VertexVariance_lineEdit->setEnabled(false);
            NormalVariance_lineEdit->setEnabled(false);
            break;
        default:
            break;
    }
}




Window::~Window()
{

   if(renderArea) delete renderArea;
   if(thread) delete thread;
}
